//
//  GolfFriendCircleCell2.m
//  GolfFriend
//
//  Created by vescky on 14-2-3.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "GolfFriendCircleCell2.h"
#import "CommonUtility.h"
#import "DateUtility.h"
#import "SVProgressHUD.h"

#import "UIImageView+MJWebCache.h"
#import "LitePhotoCollectionView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

#import "AppsEngine.h"
#import "ZanDao.h"
#import "UserSession.h"

@interface GolfFriendCircleCell2 () {
    LitePhotoCollectionView *imgCollectionView;
    CircleContentVO *contentVo;
}

@end

@implementation GolfFriendCircleCell2
@synthesize delegate,isSupported;

//201 赞
//202 评论
//210 个人资料
//220 正文详情
- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 201) {
        //201 赞
        if (isSupported) {
            [SVProgressHUD showErrorWithStatus:@"您已赞过!"];
            return;
        }
        NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:contentVo.articleId,@"articleId",@"circle",@"type", nil];
        [[AppsEngine shareAppsEngine] submitRequest:@"gfPublishZan" param:params onCompletion:^(id jsonResponse) {
            NSLog(@"zan success");
        } onError:^(NSError *error) {
            
        } method:@"GET"];
        
        //缓存到本地
        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[[UserSession shareUserSession] getUserId],@"userId",
                              @"circle",@"type",contentVo.articleId,@"recordId",nil];
        [[ZanDao shareZanDao] saveZanInfo:dict];
        isSupported = YES;
        labelZan.text = [NSString stringWithFormat:@"%d",[labelZan.text intValue]+1];
        [btnZan setSelected:YES];
        
        contentVo.zan = labelZan.text;
    }
    else if (btn.tag == 202) {
        //202 评论
    }
    else if (btn.tag == 210) {
        //210 个人资料
        
    }
    else if (btn.tag == 220) {
        //220 正文详情
        
    }
    if ([self.delegate respondsToSelector:@selector(cell2ButtonPressed:withObject:)]) {
        [self.delegate cell2ButtonPressed:sender withObject:contentVo];
    }
}

- (void)setDataInfo:(CircleContentVO*)ccVO {
    contentVo = ccVO;
    
    NSArray *imgArr,*videoArr;
    
    if (ccVO.userId && ccVO.userId.length > 0) {
        [imgvAvatar setImageURLStr:ccVO.avatar placeholder:Default_Avatar];
    }
    
    if (ccVO.userName && ccVO.userName.length > 0) {
        [labelUserName setText:ccVO.userName];
    }
    else {
        [labelUserName setText:@"匿名"];
    }
    
    if (ccVO.cityName && ccVO.cityName.length > 0) {
        [labelLocation setText:ccVO.cityName];
    }
    else {
        [imgvLocation removeFromSuperview];
        [labelLocation removeFromSuperview];
    }
    
    
    [labelZan setText:ccVO.zan];
    [labelComment setText:ccVO.comments];
    [labelDateTime setText:ccVO.time];
        
    if (ccVO.sourceLinks && [ccVO.sourceLinks count] > 0) {
        imgArr = [ccVO.sourceLinks objectForKey:@"image"];
        videoArr = [ccVO.sourceLinks objectForKey:@"video"];
    }
    
    float flexibleMargin = 5;
    CGRect contentRect = labelContent.frame;
    
    CGSize cSize = [ccVO.content sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(labelContent.frame.size.width,1000) lineBreakMode:0];
    contentRect.size.height = cSize.height >= 45 ? cSize.height : 45;
    contentRect.origin.y = flexibleMargin;
    [labelContent setText:ccVO.content];
    [labelContent setLineBreakMode:0];
    [labelContent setNumberOfLines:0];
    [labelContent setFrame:contentRect];
    [labelLocation setBackgroundColor:[UIColor clearColor]];
    
    //设置内容图片
    CGRect imgvContentRect = CGRectMake(0, contentRect.origin.y + contentRect.size.height + flexibleMargin, viewContent.frame.size.width, 200);
    imgvContent = [[UIImageView alloc] initWithFrame:imgvContentRect];
    if (imgArr && [imgArr count] > 0) {
        NSString *skImageName = [NSString stringWithFormat:@"%@?imageView2/2/w/300",[imgArr objectAtIndex:0]];//,(int)(viewContent.frame.size.width - 2*flexibleMargin)
        [imgvContent setImageURLStr:skImageName placeholder:Default_Image_Normal];
        [imgvContent setUserInteractionEnabled:YES];
        [imgvContent addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImagePre:)]];
    }
    else {
        [imgvContent removeFromSuperview];
        imgvContentRect.size.height = 0;
    }
    imgvContent.contentMode = UIViewContentModeScaleAspectFill;
    imgvContent.clipsToBounds = YES;
    [viewContent addSubview:imgvContent];
    
    //设置内容view的高度
    [viewContent setFrame:CGRectMake(viewContent.frame.origin.x, viewContent.frame.origin.y, viewContent.frame.size.width, imgvContentRect.origin.y + imgvContentRect.size.height)];
    
    [viewButtom setFrame:CGRectMake(viewButtom.frame.origin.x, viewContent.frame.origin.y + viewContent.frame.size.height, viewButtom.frame.size.width, viewButtom.frame.size.height)];
    
    //设置内容背景
//    [imgvContentBg setFrame:CGRectMake(flexibleMargin, 0, viewContent.frame.size.width - 2 * flexibleMargin, imgvContentRect.origin.y)];
    [imgvContentBg setFrame:CGRectMake(0, 0, viewContent.frame.size.width, viewContent.frame.size.height)];
    
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, viewButtom.frame.origin.y + viewButtom.frame.size.height + 8)];
    
    //设置赞的状态
    isSupported = [[ZanDao shareZanDao] checkIfZaned:[[NSDictionary alloc] initWithObjectsAndKeys:[[UserSession shareUserSession] getUserId],@"userId",@"circle",@"type",contentVo.articleId,@"recordId",nil]];
    [btnZan setSelected:isSupported];
    
    
    [btnBig setFrame:CGRectMake(contentRect.origin.x, contentRect.origin.y, contentRect.size.width, imgvContentRect.origin.y > 0 ? imgvContentRect.origin.y : viewContent.frame.size.height)];
}

- (float)getContentHeight:(CircleContentVO*)cContent {
    [self setDataInfo:cContent];
    return self.frame.size.height;
}

- (void)tapImagePre:(UITapGestureRecognizer *)tap
{
    NSArray *imgArr;
    if (contentVo.sourceLinks && [contentVo.sourceLinks count] > 0) {
        imgArr = [contentVo.sourceLinks objectForKey:@"image"];
    }

    int count = imgArr.count;
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
        NSString *url = [imgArr[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        photo.srcImageView = imgvContent; // 来源于哪个UIImageView
        [photos addObject:photo];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
}

@end
